{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random,datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "class workmate:\n",
    "    def __init__(self,name,age,clothesColor):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.clothesColor = clothesColor\n",
    "    def __str__(self):\n",
    "        return \"name:<{0}>  age:<{1}>  clothesColor:<{2}>\\\n",
    "        \".format(self.name,self.age,self.clothesColor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "colorlist = [\"red\",\"blue\",\"green\",\n",
    "            \"yellow\",\"black\",\"gray\",\n",
    "            \"white\",\"orange\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 生成一个100万条件记录的集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "arrDemo = [workmate(chr(random.randint(65,91)),\n",
    "                    random.randint(21,55),\n",
    "                    random.choice(colorlist)) for i in range(1000000)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:<Q>  age:<43>  clothesColor:<orange>        \n",
      "name:<V>  age:<21>  clothesColor:<green>        \n",
      "name:<T>  age:<33>  clothesColor:<white>        \n",
      "name:<C>  age:<47>  clothesColor:<black>        \n",
      "name:<B>  age:<47>  clothesColor:<gray>        \n",
      "name:<J>  age:<52>  clothesColor:<yellow>        \n",
      "name:<B>  age:<33>  clothesColor:<black>        \n",
      "name:<H>  age:<33>  clothesColor:<black>        \n",
      "name:<A>  age:<38>  clothesColor:<gray>        \n",
      "name:<P>  age:<37>  clothesColor:<gray>        \n"
     ]
    }
   ],
   "source": [
    "for w in arrDemo[:10]:\n",
    "    print(str(w))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 如果要在数组里面找到某个条件，只能进行遍历和判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "37037\n",
      "0:00:00.090800\n"
     ]
    }
   ],
   "source": [
    "s = datetime.datetime.now()\n",
    "v = 0\n",
    "for w in arrDemo:\n",
    "    if w.name == \"A\":\n",
    "        v +=1\n",
    "print(v)\n",
    "print(datetime.datetime.now() -s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 构造一个map，把要进行查询的条件作为key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "mapDemo = {}\n",
    "for w in arrDemo:\n",
    "    if w.name in mapDemo:\n",
    "        mapDemo[w.name].append(w)\n",
    "    else:\n",
    "        mapDemo[w.name] = [w]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 直接进行索引就可以了，效率比数组迭代，要快两个数量级。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "37037\n",
      "0:00:00.000998\n"
     ]
    }
   ],
   "source": [
    "s = datetime.datetime.now()\n",
    "print(len(mapDemo[\"A\"]))\n",
    "print(datetime.datetime.now() -s)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
